import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.Color;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class TutorLogbook extends JFrame {

	private JPanel contentPane;
	private JTextField id;
	private JTextField textField;
	String user;

	/**
	 * Launch the application.
	 */
	

	/**
	 * Create the frame.
	 */
	public TutorLogbook(final String user) {
		this.user =user;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBackground(Color.ORANGE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNewLabel_5 = new JLabel("");
		lblNewLabel_5.setIcon(new ImageIcon(LogIn.class.getResource("/resources/p-331002-georgia-tech-yellow-jackets-black-spare-tire-cover-jt-2324558420.jpg")));
		lblNewLabel_5.setBounds(476, 6, 201, 164);
		getContentPane().add(lblNewLabel_5);
		JLabel lblTutorName = new JLabel("Tutor Name:");
		lblTutorName.setBounds(22, 44, 84, 16);
		contentPane.add(lblTutorName);
		
		final JComboBox term = new JComboBox();
		term.setModel(new DefaultComboBoxModel(new String[] {"Fall 2011", "Spring 2012", "Fall 2012", "Spring 2013"}));
		term.setBounds(116, 212, 136, 27);
		contentPane.add(term);
		
		JLabel name = new JLabel("");
		name.setBounds(118, 44, 118, 16);
		contentPane.add(name);
		
		JLabel lblCourseCode = new JLabel("Course Code:");
		lblCourseCode.setBounds(22, 83, 84, 16);
		contentPane.add(lblCourseCode);
		
		final JComboBox comboBox = new JComboBox();
		comboBox.setBounds(118, 79, 118, 27);
		contentPane.add(comboBox);
		
		JLabel lblStudentId = new JLabel("Student ID:");
		lblStudentId.setBounds(22, 134, 84, 16);
		contentPane.add(lblStudentId);
		
		JLabel lblTerm = new JLabel("Term:");
		lblTerm.setBounds(22, 223, 61, 16);
		contentPane.add(lblTerm);
		
		id = new JTextField();
		id.setBounds(118, 128, 134, 28);
		contentPane.add(id);
		id.setColumns(10);
		
		JLabel lblStudentName = new JLabel("Student Name:");
		lblStudentName.setBounds(22, 180, 92, 16);
		contentPane.add(lblStudentName);
		
		textField = new JTextField();
		textField.setBounds(118, 174, 134, 28);
		contentPane.add(textField);
		textField.setColumns(10);
		
		try{
			Connection con= Connect.getConnection();
			Statement stat = con.createStatement();
			ResultSet res = stat.executeQuery("SELECT Name FROM STUDENT WHERE Username='"+ user +"'");
			while(res.next()){
				name.setText(res.getString(1));
			}
			
			
		res=stat.executeQuery("SELECT DISTINCT Code FROM CRS_CODE AS C, TUTORS_FOR AS TF, TUTOR AS T, STUDENT AS S WHERE  TF.Tutor_ID=T.Tutor_Identification AND T.Student_Username='" +user+ "' AND TF.Course_Title=C.Course_Title");
		ArrayList<String> crs = new ArrayList<String>();
		while(res.next()){
			crs.add(res.getString(1));
			
		}
		String[] course = new String[crs.size()];
		for(int i=0;i<crs.size();i++){
			course[i]=crs.get(i);
		}
		comboBox.setModel(new DefaultComboBoxModel(course));
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try{
					Connection con = Connect.getConnection();
					Statement stat = con.createStatement();
					String student="-1";
					int crn=0;
					int tutor_id =0;
					String iid="";
					if(id.getText().equals("")){
						iid="1";
					}
					else{
						iid=id.getText();
					}
					ResultSet rs = stat.executeQuery("SELECT Username FROM STUDENT WHERE ID='"+ Integer.parseInt(iid) +"'");
					while(rs.next()){
						student=rs.getString(1);
					}
					rs=stat.executeQuery("SELECT SC.CRN FROM SECTION AS SC, REGISTERS AS R, CRS_CODE AS CR WHERE CR.Code='"+comboBox.getSelectedItem().toString()+"' AND CR.Course_Title=SC.Course_Title AND SC.CRN=R.Section_CRN AND R.Student_Username='"+ student+"' AND Term='"+term.getSelectedItem().toString()+"'");
					while(rs.next()){
						crn=rs.getInt(1);
					}
					if(student.equals("-1")){
						JOptionPane.showMessageDialog(null,"Enter Valid Student ID");
					}else if(crn==0){
						JOptionPane.showMessageDialog(getContentPane(),"Enter a Course, '"+student+"' is currently taking");
					}
					else{
						
						rs=stat.executeQuery("SELECT Tutor_Identification FROM TUTOR WHERE Student_Username='"+user+"'");
					while(rs.next()){
						tutor_id=rs.getInt(1);
					}
					java.util.Date date= new java.util.Date();
					
					stat.executeUpdate("INSERT INTO LOG_VISIT VALUES('"+student+"','"+crn+"','"+ new Timestamp(date.getTime())+"','" + tutor_id +"')");
					dispose();
					new StudentServices(user);
					}
					}
				catch(Exception e){
					//JOptionPane.showMessageDialog(getContentPane(),"Check Connection");	
				}
				
			}
		});
		btnSubmit.setBounds(449, 210, 117, 29);
		contentPane.add(btnSubmit);
		
		JButton btnStudentServices = new JButton("Student Services");
		btnStudentServices.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
				new StudentServices(user);
			}
		});
		btnStudentServices.setBounds(578, 210, 136, 29);
		contentPane.add(btnStudentServices);
		
		
		
		
		}catch(Exception e){
			//JOptionPane.showMessageDialog(getContentPane(),"Check Connection");	
		}
		
		setPreferredSize(new Dimension(800, 300));
		pack();
		setVisible(true);
		setTitle("Tutor Logbook");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}
